package gui.NetworkManager;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;

import control.CurrentSituationController;
import control.LoginController;

import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.toedter.calendar.JDateChooser;
import javax.swing.DefaultComboBoxModel;
/**
 * this class use to select the type of Report
 * @author G8
 *
 */
public class SituationScreen extends JPanel implements ActionListener{
	public JButton btnLogout;
	public JButton btnBack;
	private JButton btnPDF;
	private JButton btnGraphic;
	/**
	 * Situation Screen Constructor 
	 */
	public SituationScreen(){
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize() {
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Current Situation", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(ReadOnlyDataScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		JLabel lblSituMode = new JLabel("Select current situation viewing mode :");
		lblSituMode.setBounds(69, 96, 402, 14);
		add(lblSituMode);
		
		btnPDF = new JButton("PDF");
		btnPDF.setBounds(69, 121, 89, 35);
		btnPDF.addActionListener(this);
		add(btnPDF);
		
		btnGraphic = new JButton("Graphic");
		btnGraphic.setBounds(69, 167, 89, 35);
		btnGraphic.addActionListener(this);
		add(btnGraphic);
	}
	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnPDF){
			CurrentSituationController currSituController = new CurrentSituationController(null);
			SystemTypes lotToHandle = new SystemTypes(VCP.currentLot,null);
			SystemTypes snapshotResult = currSituController.getSnapshot(lotToHandle, "PDF");
			if (!snapshotResult.getSysMode().equals(SystemMode.OK))
				JOptionPane.showMessageDialog(null,snapshotResult.getObj().toString() , "Error",
	                    JOptionPane.ERROR_MESSAGE);
		}
		if (e.getSource() == btnGraphic){
			CurrentSituationController currSituController = new CurrentSituationController(null);
			SystemTypes lotToHandle = new SystemTypes(VCP.currentLot,null);
			SystemTypes snapshotResult = currSituController.getSnapshot(lotToHandle, "Graphic");
			if (!snapshotResult.getSysMode().equals(SystemMode.OK))
				JOptionPane.showMessageDialog(null,snapshotResult.getObj().toString() , "Error",
	                    JOptionPane.ERROR_MESSAGE);
		}
		if (e.getSource() == btnBack){
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout){
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
